package edu.sdwu.liqing.qasystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.sdwu.liqing.qasystem.annotation.HasRole;
import edu.sdwu.liqing.qasystem.dto.QaQuestionDto;
import edu.sdwu.liqing.qasystem.entity.QaQuestion;
import edu.sdwu.liqing.qasystem.model.Response;
import edu.sdwu.liqing.qasystem.service.IQaQuestionService;
import edu.sdwu.liqing.qasystem.vo.QaQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 问题表  前端控制器
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@RestController
@RequestMapping("api/qa-question")
public class QaQuestionController {

    @Autowired
    IQaQuestionService questionService;


    @PostMapping
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> save(@RequestBody QaQuestion qaQuestion) {
        if (questionService.save(qaQuestion)) {
            return Response.ok("提问成功");
        }
        return Response.error("提问失败");
    }

    @PutMapping
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> update(@RequestBody QaQuestion question) {
        if (questionService.update(question)) {
            return Response.ok("更新成功");
        }
        return Response.error("更新失败");
    }

    @DeleteMapping("{id}")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> delete(@PathVariable("id") String id) {
        if (questionService.delete(id)) {
            return Response.ok("删除成功");
        }
        return Response.error("删除失败");
    }

    @PostMapping("page")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<Page<QaQuestion>> page(@RequestBody QaQuestionVo vo) {
        return Response.ok(questionService.page(vo));
    }

    @GetMapping("{id}")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<QaQuestionDto> get(@PathVariable("id") String id) {
        return Response.ok(questionService.get(id));
    }


    @PostMapping("current/page")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<Page<QaQuestion>> currentPage(@RequestBody QaQuestionVo vo) {
        return Response.ok(questionService.currentPage(vo));
    }

    @PutMapping("solved/{id}")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<Object> setSolved(@PathVariable("id") String id) {
        if (questionService.solved(id)) {
            return Response.ok("设置成功");
        }
        return Response.error("设置失败,请查看问题是否有答案！");
    }

    @GetMapping("es/list")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<List<QaQuestion>> getList(@RequestParam String keyword) {
        return Response.ok(questionService.list(keyword));
    }

}

